package cn.tedu.autoshow.passport.service.impl;

import cn.tedu.autoshow.commons.ex.ServiceException;
import cn.tedu.autoshow.commons.web.ServiceCode;
import cn.tedu.autoshow.passport.mapper.PermissionMapper;
import cn.tedu.autoshow.passport.mapper.RoleMapper;
import cn.tedu.autoshow.passport.mapper.RolePermissionMapper;
import cn.tedu.autoshow.passport.pojo.entity.Permission;
import cn.tedu.autoshow.passport.pojo.entity.Role;
import cn.tedu.autoshow.passport.pojo.entity.RolePermission;
import cn.tedu.autoshow.passport.service.IRolePermissionService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class RolePermissionServiceImpl implements IRolePermissionService {

    @Autowired
    private RolePermissionMapper mapper;
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private PermissionMapper permissionMapper;

    @Override
    public void insertsRolePermissionByRoleId(Long roleId, Long[] permissionIds) {
        log.info("开始:【配置角色权限数据】");
        log.debug("传入参数,roleId={}", roleId);
        //检查角色ID检查角色是否存在数据，如果不存在，则抛出异常
        QueryWrapper<Role> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("id", roleId);
        int countById = roleMapper.selectCount(queryWrapper2);
        log.info("规则：根据角色id检索数据库结果,countById={}", countById);
        if(countById == 0){
            log.info("结果2:根据角色id检索数据量为0，抛出异常...");
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, "新增角色权限失败，角色数据不存在！");
        }

        log.info("新增角色权限关系数据...");
        RolePermission[] rolePermissions = new RolePermission[permissionIds.length];
        LocalDateTime now = LocalDateTime.now();
        for (int i = 0; i < rolePermissions.length; i++) {
            RolePermission rolePermission = new RolePermission();
            rolePermission.setRoleId(roleId).setPermissionId(permissionIds[i])
                    .setGmtCreate(now).setGmtModified(now);
            rolePermissions[i] = rolePermission;
        }
        log.debug("新增角色权限关系数据,rolePermissions={}", rolePermissions);
        int rows = mapper.insertBatch(rolePermissions);
        if(rows != permissionIds.length){
            String message = "新增角色权限关系失败，服务器忙，请稍后再试！";
            log.info("结果1:数据返回行数不满足预估行数，预估={}, 实际={}, 抛出自定异常...", permissionIds.length, rows);
            throw new ServiceException(ServiceCode.ERR_INSERT, message);
        }
        log.info("结果1:新增角色权限关系结果,rows={}", rows);
    }

    @Override
    public void insertsRolePermissionByPermissionId(Long permissionId, Long[] roleIds) {
        log.info("开始:【配置角色权限数据】");
        log.debug("传入参数,permissionId={}", permissionId);
        //检查角色ID检查角色是否存在数据，如果不存在，则抛出异常
        QueryWrapper<Permission> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("id", permissionId);
        int countById = permissionMapper.selectCount(queryWrapper2);
        log.info("规则：根据权限id检索数据库结果,countById={}", countById);
        if(countById == 0){
            log.info("结果2:根据权限id检索数据量为0，抛出异常...");
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, "配置角色权限数据失败，权限数据不存在！");
        }

        log.info("新增角色权限关系数据...");
        RolePermission[] rolePermissions = new RolePermission[roleIds.length];
        LocalDateTime now = LocalDateTime.now();
        for (int i = 0; i < rolePermissions.length; i++) {
            RolePermission rolePermission = new RolePermission();
            rolePermission.setRoleId(roleIds[i]).setPermissionId(permissionId)
                    .setGmtCreate(now).setGmtModified(now);
            rolePermissions[i] = rolePermission;
        }
        log.debug("新增角色权限关系数据,rolePermissions={}", rolePermissions);
        int rows = mapper.insertBatch(rolePermissions);
        if(rows != roleIds.length){
            String message = "新增角色权限关系失败，服务器忙，请稍后再试！";
            log.info("结果1:数据返回行数不满足预估行数，预估={}, 实际={}, 抛出自定异常...", roleIds.length, rows);
            throw new ServiceException(ServiceCode.ERR_INSERT, message);
        }
        log.info("结果1:新增角色权限关系结果,rows={}", rows);
    }

    @Override
    public void deletePermissionByRole(Long roleId, Long[] permissionIds) {
        log.info("开始:【删除角色权限关系数据】");
        log.debug("传入参数,roleId={}", roleId);
        //检查角色ID检查角色是否存在数据，如果不存在，则抛出异常
        QueryWrapper<Role> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("id", roleId);
        int countById = roleMapper.selectCount(queryWrapper2);
        log.info("规则：根据角色id检索数据库结果,countById={}", countById);
        if(countById == 0){
            log.info("结果2:根据角色id检索数据量为0，抛出异常...");
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, "删除角色权限关系失败，角色数据不存在！");
        }

        log.info("删除角色权限关系数据...");
        log.debug("删除角色权限关系数据,roleId={},permissionIds={}", roleId, permissionIds);
        int rows = mapper.deletePermissionByRole(roleId, permissionIds);
        if(rows != permissionIds.length){
            String message = "删除角色权限关系数据，服务器忙，请稍后再试！";
            log.info("结果1:数据返回行数不满足预估行数，预估={}, 实际={}, 抛出自定异常...", permissionIds.length, rows);
            throw new ServiceException(ServiceCode.ERR_INSERT, message);
        }
        log.info("结果1:删除角色权限关系结果,rows={}", rows);
    }

    @Override
    public void deleteRoleByPermission(Long permissionId, Long[] roleIds) {
        log.info("开始:【删除角色权限关系数据】");
        log.debug("传入参数,permissionId={}", permissionId);
        //检查角色ID检查角色是否存在数据，如果不存在，则抛出异常
        QueryWrapper<Permission> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("id", permissionId);
        int countById = permissionMapper.selectCount(queryWrapper2);
        log.info("规则：根据角色id检索数据库结果,countById={}", countById);
        if(countById == 0){
            log.info("结果2:根据角色id检索数据量为0，抛出异常...");
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, "删除角色权限关系失败，权限数据不存在！");
        }

        log.info("删除角色权限关系数据...");
        log.debug("删除角色权限关系数据,permissionId={},roleIds={}", permissionId, roleIds);
        int rows = mapper.deleteRoleByPermission(permissionId, roleIds);
        if(rows != roleIds.length){
            String message = "删除角色权限关系数据，服务器忙，请稍后再试！";
            log.info("结果1:数据返回行数不满足预估行数，预估={}, 实际={}, 抛出自定异常...", roleIds.length, rows);
            throw new ServiceException(ServiceCode.ERR_INSERT, message);
        }
        log.info("结果1:删除角色权限关系结果,rows={}", rows);
    }

    @Override
    public List<Long> permissionListByRoleId(Long roleId) {
        log.info("开始:【根据roleId查询角色下有多少权限】");
        log.debug("传入参数,roleId={}", roleId);
        List<RolePermission> rolePermissions = mapper.permissionListByRoleId(roleId);
        List<Long> permissionIds = new ArrayList<>();
        for (RolePermission rolePermission : rolePermissions) {
            permissionIds.add(rolePermission.getPermissionId());
        }

        log.info("结果1:查询出数据permissionIds={}", permissionIds);
        return permissionIds;
    }

    @Override
    public List<Long> roleListByPermissionId(Long permissionId) {
        log.info("开始:【根据permissionId查询权限下有多少角色】");
        log.debug("传入参数,permissionId={}", permissionId);
        List<RolePermission> rolePermissions = mapper.roleListByPermissionId(permissionId);
        List<Long> roleIds = new ArrayList<>();
        for (RolePermission rolePermission : rolePermissions) {
            roleIds.add(rolePermission.getRoleId());
        }

        log.info("结果1:查询出数据roleIds={}", roleIds);
        return roleIds;
    }
}
